package com.ds.bpm.engine.proxy;

import com.ds.bpm.client.ActivityDefEvent;
import com.ds.bpm.engine.BPMException;
import com.ds.bpm.engine.BPMServer;
import com.ds.bpm.engine.IOTEventEngine;
import com.ds.bpm.engine.database.event.DbActivityDefEvent;
import com.ds.bpm.enums.activitydef.ActivityDefDeadLineOperation;
import com.ds.bpm.enums.event.DeviceAPIEventEnums;
import com.ds.common.JDSException;
import com.ds.enums.DurationUnit;
import com.ds.iot.DeviceEndPoint;
import com.ds.iot.enums.DeviceDataTypeKey;

import java.util.ArrayList;
import java.util.List;

public class ActivityDefEventProxy implements ActivityDefEvent {

    private IOTEventEngine eventEngine;
    private DbActivityDefEvent event;
    private DeviceDataTypeKey attributeName;

    public DbActivityDefEvent getEvent() {
        return event;
    }

    public void setEvent(DbActivityDefEvent event) {
        this.event = event;
    }

    public DeviceDataTypeKey getAttributeName() {
        return attributeName;
    }

    public void setAttributeName(DeviceDataTypeKey attributeName) {
        this.attributeName = attributeName;
    }

    public ActivityDefEventProxy(DbActivityDefEvent event, String systemCode) {

        try {
            this.eventEngine = (IOTEventEngine) BPMServer.getEventEngine(systemCode);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        this.event = event;
    }

    @Override
    public DeviceAPIEventEnums getDeviceEvent() {
        return event.getDeviceEvent();
    }

    @Override
    public String getEndpointSelectedId() {
        return event.getEndpointSelectedId();
    }

    @Override
    public List<DeviceEndPoint> getEndpoints() {
        try {
            return this.eventEngine.getParticipant(event.getEndpointSelectedAtt(), null);
        } catch (BPMException e) {
            e.printStackTrace();
        }
        return new ArrayList<DeviceEndPoint>();
    }

    @Override
    public DurationUnit getDurationUnit() {
        return event.getDurationUnit();
    }

    @Override
    public String getAlertTime() {
        return event.getAlertTime();
    }

    @Override
    public ActivityDefDeadLineOperation getDeadLineOperation() {
        return event.getDeadLineOperation();
    }
}
